<?php

namespace App\Http\Controllers;

use App\Articles;
use App\Members;
use Illuminate\Http\Request;

class ArticlesController extends Controller
{
    public function add(Request $request){
        $data = $request->all();
        //校验用户信息是否有误
        $user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }

        // 验证签名
        $res = $this->checkSign($data['sign']);
        if(!$res){
            return json_encode(['status'=>0,'msg'=>'验证签名 error']);
        }
        // 提交主要信息
        $addData = array(
            'art_title'      => $data['title'],
            'art_uid'        => $data['uid'],
            'art_cate'       => intval($data['cate']),
            'art_content'    => $data['content'],
            'art_createtime' => time()
        );
        $res = Articles::insert($addData);
        if(!$res){
            return json_encode(['status'=>0,'msg'=>'服务器忙请重试']);
        }
        // 更新会员积分
        Members::where('u_id',$data['uid'])->increment('u_integral', 10);
        return json_encode(['status'=>'ok','msg'=>'good boy']);
    }

    public function getList(Request $request){
        $data = $request->all();
        $user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }

        $page = empty($data['page']) ? 1 : intval($data['page']);
        $list = Articles::offset(10*($page-1))->limit(2)->get();
        if($list->isEmpty()){
            return json_encode(['status'=>'empty']);
        }
        return json_encode(['status'=>'ok','data'=>$list]);
    }

    public function getCateList(Request $request){
        $data = $request->all();
        //不登录，不用校验
        /*$user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }*/

        $page = empty($data['page']) ? 1 : intval($data['page']);
        $cate = empty($data['cate']) ? 0 : intval($data['cate']);
        if(empty($cate)){
            $list = Articles::offset(10*($page-1))->limit(2)->get();
        }else{
            $list = Articles::where('art_cate',$cate)->offset(10*($page-1))->limit(2)->get();
        }
        if($list->isEmpty()){
            return json_encode(['status'=>'empty']);
        }
        return json_encode(['status'=>'ok','data'=>$list]);
    }

    public function del(Request $request){
        $data = $request->all();
        $user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }

        if(empty($data['artId'])){
            return json_encode(['status'=>0,'art id empty ...']);
        }

        // 查询文章
        $art = Articles::where('art_id',$data['artId'])->where('art_uid',$user['u_id'])->first();
        if(!$art){
            return json_encode(['status'=>0,'msg'=>'art id error ...']);
        }
        // 删除
        $res = Articles::where('art_id',$data['artId'])->delete();
        if(!$res){
            return json_encode(['status'=>0,'msg'=>'del is fail']);
        }
        // 扣除积分
        Members::where('u_id',$data['uid'])->decrement('u_integral', 10);
        return json_encode(['status'=>'ok']);
    }

    public function show(Request $request){
        $data = $request->all();
        //不登录，不用校验
        /*$user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }*/

        if(empty($data['artId'])){
            return json_encode(['status'=>0,'art id empty ...']);
        }

        $article = Articles::where('art_id',$data['artId'])->leftJoin('members', 'articles.art_uid', '=', 'members.u_id')->first();
        return json_encode(['status'=>'ok','data'=>$article]);
    }
    public function edit(Request $request){
        $data = $request->all();
        $user = $this->checkUser($data['uid'],$data['random']);
        if(!$user){
            return json_encode(['status'=>false,'msg'=>'用户信息有误']);
        }

        // 验证签名
        $res = $this->checkSign($data['sign']);
        if(!$res){
            return json_encode(['status'=>0,'msg'=>'验证签名 error']);
        }
        // 检查文章
        if(empty($data['artId'])){
            return  json_encode(['status'=>0,'msg'=>'article id is empty']);
        }
        $article = Articles::where('art_id',$data['artId'])->where('art_uid',$data['uid'])->first();
        if(empty($article)){
            return json_encode(['status'=>0,'msg'=>'art data error']);
        }
        $insertData = array(
            'art_id' => $data['artId'],
            'art_title'      => $data['title'],
            'art_uid'        => $user['u_id'],
            'art_cate'       => intval($data['cate']),
            'art_content'    => $data['content']
        );
        Articles::where('art_id',$data['artId'])->update($insertData);
        return json_encode(['status'=>'ok','msg'=>'update is success']);
    }
}
